package com.example.cstr.controller;

import com.example.cstr.entity.Message;
import com.example.cstr.entity.User;
import com.example.cstr.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/User")
@Api(tags = "用户")
public class UserController {
    @Resource
    private UserService userService;

    @ApiOperation(value="添加用户")
    @PostMapping("/add")
    public Message insertUser(User user){
        return userService.insertSelective(user)>0?Message.success(null):Message.fail(null);
    }

    @ApiOperation(value = "删除用户")
    @DeleteMapping("/delete")
    public Message deleteUser(Integer workId){
        return userService.deleteByPrimaryKey(workId)>0?Message.success(null):Message.fail(null);
    }

    @ApiOperation(value = "修改用户")
    @PutMapping("/update")
    public Message updateUser(User user){
        return userService.updateByPrimaryKeySelective(user)>0?Message.success(null):Message.fail(null);
    }

    @ApiOperation(value="根据干部工号查询用户")
    @GetMapping("/select")
    public Message selectUser(Integer workId){
        return userService.selectByPrimaryKey(workId)!=null?Message.success(null).add(userService.selectByPrimaryKey(workId)):Message.fail(null);
    }

    @ApiOperation(value = "查询所有用户")
    @GetMapping("/selectAll")
    public Message selectAllUser(){
        return Message.success(null).add(userService.selectAllUser());
    }

    @ApiOperation(value = "登录")
    @PostMapping("/Login")
    public Message Login(Integer workId,String userPwd){
        return userService.Login(workId,userPwd);
    }

    @ApiOperation(value = "根据token获取用户信息")
    @GetMapping("/info")
    public Message getUserByToken(String token){
        return userService.getUserByToken(token);
    }

    @ApiOperation(value = "分页查询-查询所有用户")
    @GetMapping("/pagingSearch")
    public Message PagingSearch(@RequestParam Integer pageNum,@RequestParam Integer size){
        return userService.PagingSearch(pageNum,size);
    }

    @ApiOperation(value = "注销")
    @PostMapping("/logout")
    public Message logout(){
        return Message.success("注销成功!");
    }

    @ApiOperation(value = "全部用户数据排行展示（根据总学时由高到低排序）")
    @GetMapping("/sortAllUser")
    public Message sortAllUser(){
        return Message.success(null).add(userService.sortAllUser());
    }

    @ApiOperation(value = "用户个人数据排行展示（根据总学时由高到低排序）")
    @GetMapping("/sortOwnUser")
    public Message sortOwnUser(Integer workId){
        return userService.sortOwnUser(workId)!=null?Message.success(null).add(userService.sortOwnUser(workId)):Message.fail("该用户不存在！");
    }
}
